#include "../unp.h"
#include <time.h>

int 
main(int argc, char **argv)
{
    pid_t pid;
    int listenfd, connfd;
    struct sockaddr_in servaddr;
    char buff[MAXLINE];
    listenfd = Socket(AF_INET, SOCK_STREAM, 0);

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family=AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(13);

    Bind(listenfd,(SA*) &servaddr, sizeof(servaddr));

    Listen(listenfd, LISTENQ);

    for(;;)
    {
        connfd = Accept(listenfd,(SA*)NULL, NULL);
        if((pid = fork()) == 0)
        {
            Close(listenfd);

            /** do something begin */
            time_t ticks = time(NULL);
            snprintf(buff, sizeof(buff),"%.24s\r\n",ctime(&ticks));
            Write(connfd, buff, strlen(buff));
            /** do something end */
            Close(connfd);
            exit(0);
        }
        Close(connfd);
    }


}